<?php
class TourController extends Apps_Controller_Action_Default {
	public function indexAction() {
	}

	public function detailAction() {
        $id = intval($this->_getParam('id'));
        $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'tour', 'field' => 'id'));
        if(!$id || $validator->isValid($id)) {
            $this->_helper->redirector('index', 'home');
        } else {
            $tourModel = new M_Tour();
            $tour      = $tourModel->getTourById($id)->query()->fetch();

            //get table price
            $tbTourHotel      = new M_DbTable_TourHotel();
            $tbTourHotelPrice = new M_DbTable_TourHotelPrice();

            $tourHotel = $tbTourHotel->fetchAll(array('tour_id =?' => $id))->toArray();
            foreach($tourHotel as $k => $v) {
                $tourHotel[$k]['price'] = $tbTourHotelPrice->listTourHotelPriceByTourHotelId($v['id'])->query()->fetchAll();
            }
            //Trip in style : get trip style of Vietnam ; counttry_id = 1
            $tourStyle     = new M_TourStyle();
            $this->view->listTourStyle = $tourStyle->listStyleShowHome()->query()->fetchAll();

            $this->view->tour       = $tour;
            $this->view->tourHotel  = $tourHotel;
            $this->view->columnLeft = $this->columnLeftTourRender();

            //meta tag
            $tbVctSetting = new M_DbTable_VctSetting();
            $vctSetting   = $tbVctSetting->getMetaTag();
            $this->_page_meta_keywords    = $tour['meta_keyword'] ? $tour['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
            $this->_page_meta_description = $tour['meta_description'] ? $tour['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
            $this->_page_title 	  		  = $tour['header_title'] ? $tour['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);
        }
	}

	public function categoryAction() {
        $rq = $this->_request;
        $id = intval($rq->getParam('id'));
        $countryId = intval($rq->getParam('c'));

        $validator  = new Zend_Validate_Db_NoRecordExists(array('table' => 'tour_category', 'field' => 'id'));
        if(!$id || $validator->isValid($id) || !$countryId) {
            $this->goToControllerIndex();
        } else {
            $tourCategoryModel = new M_TourCategory();
            $tourCategory = $tourCategoryModel->getTourCategoryById($id)->query()->fetch();
            $this->view->tourCategory = $tourCategory;

            $tourModel = new M_Tour();
            $listTour  = $tourModel->listTourByCategoryId($id, $countryId);
            if($listTour) {
                $this->setPagination($listTour, $rq);
            }

            $tbCountry = new M_Country();
            $country = $tbCountry->getCountryById($countryId)->query()->fetch();
            $this->view->country = array('id' => $countryId, 'name' => $country['name']);

            $this->view->columnLeft = $this->columnLeftTourRender();

            //Trip in style : get trip style of Vietnam ; counttry_id = 1
            $tourStyle     = new M_TourStyle();
            $this->view->listTourStyle = $tourStyle->listStyleShowHome()->query()->fetchAll();
        }
        $this->view->id = $id;

        //meta tag
        $tbVctSetting = new M_DbTable_VctSetting();
        $vctSetting   = $tbVctSetting->getMetaTag();
        $this->_page_meta_keywords    = $tourCategory['meta_keyword'] ? $tourCategory['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
        $this->_page_meta_description = $tourCategory['meta_description'] ? $tourCategory['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
        $this->_page_title 	  		  = $tourCategory['header_title'] ? $tourCategory['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);
	}

	public function packageAction() {
        $rq = $this->_request;
        $countryId = intval($rq->getParam('c'));

        $validator  = new Zend_Validate_Db_NoRecordExists(array('table' => 'country', 'field' => 'id'));
        if(!$countryId || $validator->isValid($countryId)){
            $this->goToControllerIndex();
        } else {
            $tbCountry = new M_Country();
            $country = $tbCountry->getCountryById($countryId)->query()->fetch();

            $tourModel = new M_Tour();
            $listTour  = $tourModel->listTourByCountryId($countryId);
            if($listTour) {
                $this->setPagination($listTour, $rq);
            }

            $this->view->country = array('id' => $countryId, 'name' => $country['name']);
            $this->view->columnLeft = $this->columnLeftTourRender($countryId);

            //Trip in style : get trip style of Vietnam ; counttry_id = 1
            $tourStyle     = new M_TourStyle();
            $this->view->listTourStyle = $tourStyle->listStyleShowHome()->query()->fetchAll();
        }

        //meta tag
        $tbVctSetting = new M_DbTable_VctSetting();
        $vctSetting   = $tbVctSetting->getMetaTag();
        $this->_page_meta_keywords    = $vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword;
        $this->_page_meta_description = $vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description;
        $this->_page_title 	  		  = $vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title;
	}

	//trip in style
	public function tripInStyleAction() {
	    $rq = $this->_request;
	    $id = intval($rq->getParam('id'));
	    $countryId  = intval($rq->getParam('c'));

	    $validator  = new Zend_Validate_Db_NoRecordExists(array('table' => 'tour_style', 'field' => 'id'));
	    if(!$id || $validator->isValid($id)) {
            $this->goBack();
	    } else {
            $tourModel = new M_Tour();
            $listTour = $tourModel->listTourByStyleId($id);
            if($listTour) {
                $this->setPagination($listTour, $rq);
            }

            $tourStyleModel = new M_TourStyle();
            $tourStyleById = $tourStyleModel->getTourStyleById($id)->query()->fetch();
            $this->view->tourStyle = $tourStyleById;

            $this->view->columnLeft = $this->columnLeftTourRender();

            $this->view->id = $id;

            //Trip in style : get trip style of Vietnam ; counttry_id = 1
            $tourStyle     = new M_TourStyle();
            $this->view->listTourStyle = $tourStyle->listStyleShowHome()->query()->fetchAll();

            $tbCountry = new M_Country();
            $country = $tbCountry->getCountryById($countryId)->query()->fetch();
            $this->view->country = array('id' => $countryId, 'name' => $country['name']);

            //meta tag
            $tbVctSetting = new M_DbTable_VctSetting();
            $vctSetting   = $tbVctSetting->getMetaTag();
            $this->_page_meta_keywords    = $tourStyleById['meta_keyword'] ? $tourStyleById['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
            $this->_page_meta_description = $tourStyleById['meta_description'] ? $tourStyleById['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
            $this->_page_title 	  		  = $tourStyleById['header_title'] ? $tourStyleById['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);
	    }
	}

	public function columnLeftTourRender($countryId = null) {
	    $countryModel        = new M_Country();
	    $tourCategoryModel   = new M_TourCategory();
	    $cruiseCategoryModel = new M_CruiseCategory();

	    $columnLeft   = array();
	    $listCountry  = $countryModel->listCountry()->query()->fetchAll();
	    if($listCountry) {
	        $columnLeft = $listCountry;
	        foreach($listCountry as $key => $value) {
	        	if($countryId) {
	        		if($countryId == $value['id']) {
		                $columnLeft[$key]['list_tour_category'] = $tourCategoryModel->listTourCategoryByCountryId($value['id'])
		                                                                            ->query()->fetchAll();
		                $columnLeft[$key]['list_cruise_category'] = $cruiseCategoryModel->listCruiseCategoryByCountryId($value['id'])
		                                                                                ->query()->fetchAll();
		                foreach($listCountry as $k => $v) {
		                	if($k != $key) {
		                		unset($columnLeft[$k]);
		                	}
		                }
		                return $columnLeft;
	        		}
	        	} else {
	                //list tour category
	                $columnLeft[$key]['list_tour_category'] = $tourCategoryModel->listTourCategoryByCountryId($value['id'])
	                                                                            ->query()->fetchAll();
	
	                //list cruise category
	                $columnLeft[$key]['list_cruise_category'] = $cruiseCategoryModel->listCruiseCategoryByCountryId($value['id'])
	                                                                                ->query()->fetchAll();
	        	}
	        }
	    }
        return $columnLeft;
	}
}